/* internal/js/patron/form.js */

function resetFields () {
	document.getElementById('id').value = '';
	document.getElementById('firstname').value = '';
	document.getElementById('lastname').value = '';
	document.getElementById('phone').value = '';
	document.getElementById('username').value = '';
	document.getElementById('password').value = '';
	document.getElementById('email').value = '';
	document.getElementById('street').value = '';
	document.getElementById('city').value = '';
	document.getElementById('province').value = '';
	document.getElementById('postalcode').value = '';
	selectResultRow(0);
}

function startResultsTable () {
	var html = '<table class="searchResults">' +
	           '<tr>' +
	           '<th>Name</th>' +
	           '<th>Phone</th>' +
	           '<th>Email</th>' +
	           '<th>Username</th>' +
	           '</tr>';
	return html;
}

function addResultRow (id, firstname,lastname,phone, username, email, street,city,province,postalcode) {
	var html =
		'<form id="listPatronForm' + id + '">' +
		'<input id="firstname" value="' + firstname + '" type="hidden" />' +
		'<input id="lastname" value="' + lastname + '" type="hidden" />' +
		'<input id="phone" value="' + phone + '" type="hidden" />' +
		'<input id="username" value="' + username + '" type="hidden" />' +
		'<input id="email" value="' + email + '" type="hidden" />' +
		'<input id="street" value="' + street + '" type="hidden" />' +
		'<input id="city" value="' + city + '" type="hidden" />' +
		'<input id="province" value="' + province + '" type="hidden" />' +
		'<input id="postalcode" value="' + postalcode + '" type="hidden" />' +
		'</form>' +
		'<tr id="listPatronRow' + id + '" class="searchResultsRow" onclick="showResult(\'' + id + '\')">' +
		'<td>' + firstname +' '+ lastname + '</td>' +
		'<td>' + phone + '</td>' +
		'<td>' + email + '</td>' +
		'<td>' + username + '</td>' +
		'</tr>';
	return html;
}

function endResultsTable (ids) {
	var html =
		'<input id="resultsIds" value="' + ids.join() +'" type="hidden" />' + 
		'</table>';
	return html;
}

function selectResultRow (id) {
	var ids = document.getElementById('resultsIds').value;
	if(ids !='')
	{
		var idarray = ids.split(',');
		var count = ids.length;
		for (var loop = 0; loop < count; loop++) {
			try {
				document.getElementById('listPatronRow' + idarray[loop]).className = 'searchResultsRow';
			} catch (e) {
			}
		}
		try {
			document.getElementById('listPatronRow' + id).className = 'selectedResultsRow';
		} catch (e) {
		}
	}
}

function showResult (id) {
	selectResultRow(id);
	
	var frm = document.getElementById('listPatronForm' + id);
	
	document.getElementById('id').value = id;
	document.getElementById('firstname').value = frm.firstname.value;
	document.getElementById('lastname').value = frm.lastname.value;
	document.getElementById('phone').value = frm.phone.value;
	document.getElementById('username').value = frm.username.value;
	document.getElementById('password').value = 'j8&#4i^b/1!c';
	document.getElementById('email').value = frm.email.value;
	document.getElementById('street').value = frm.street.value;
	document.getElementById('city').value = frm.city.value;
	document.getElementById('province').value = frm.province.value;
	document.getElementById('postalcode').value = frm.postalcode.value;
	//clearResults();
}



function loadPage () {
	
}

function buildTable (patrons) {
	var html = startResultsTable();
	var count = patrons.childNodes.length;
	var loop;
	var id;
	var ids = new Array(count);
	for (loop = 0; loop < count; loop++) {
		var patron = patrons.childNodes[loop];
		id = patron.getElementsByTagName('id')[0].childNodes[0].nodeValue;
		ids[loop] = id;
		
		var firstname = patron.getElementsByTagName('firstname')[0].childNodes[0].nodeValue;
		var lastname = patron.getElementsByTagName('lastname')[0].childNodes[0].nodeValue;
		var phone = patron.getElementsByTagName('phone')[0].childNodes[0].nodeValue;
		var username = patron.getElementsByTagName('username')[0].childNodes[0].nodeValue;
		var email = patron.getElementsByTagName('email')[0].childNodes[0].nodeValue;
		
		if(patron.getElementsByTagName('street')[0].childNodes.length > 0)
			var street = patron.getElementsByTagName('street')[0].childNodes[0].nodeValue;
		else
			street = '';
		
		var city = patron.getElementsByTagName('city')[0].childNodes[0].nodeValue;
		var province = patron.getElementsByTagName('province')[0].childNodes[0].nodeValue;
		var postalcode = patron.getElementsByTagName('postalcode')[0].childNodes[0].nodeValue;
		html += addResultRow(id, firstname,lastname,phone, username, email, street,city,province,postalcode);
	}
	html += endResultsTable(ids);
	return html;
}
